#include <stdio.h>
#include <stdlib.h>

int n, pre[1005]; // n表示节点数，pre存储前序遍历序列

typedef struct treenode
{
    int data;
    struct treenode *left, *right;
} tree;

// 创建并初始化一个新节点
tree *create_node(int value)
{
    tree *new_node = (tree *)malloc(sizeof(tree));
    new_node->data = value;
    new_node->left = NULL;
    new_node->right = NULL;
    return new_node;
}

// 释放二叉树的内存
void free_tree(tree *node)
{
    if (node == NULL)
        return;
    free_tree(node->left);
    free_tree(node->right);
    free(node);
}

// 递归建立二叉搜索树或其镜像树
void stablish(tree *t, int left, int right, int is_mirror)
{
    if (left > right)
        return;

    int root_val = t->data;
    int r = -1;

    // 确定右子树的起始位置
    for (int i = left; i <= right; i++)
    {
        int condition = is_mirror ? pre[i] < root_val : pre[i] >= root_val;
        if (r == -1 && condition)
            r = i;
        if (r != -1 && !condition)
            return; // 违反BST或镜像BST的规则，直接返回
    }

    // 右子树存在，创建右子树
    if (r != -1)
    {
        t->right = create_node(pre[r]);
        stablish(t->right, r + 1, right, is_mirror);
    }

    // 左子树存在，创建左子树
    if (r != left && r != -1)
    {
        t->left = create_node(pre[left]);
        stablish(t->left, left + 1, r - 1, is_mirror);
    }
    else if (r == -1)
    { // 如果没有右子树，所有的节点都是左子树
        t->left = create_node(pre[left]);
        stablish(t->left, left + 1, right, is_mirror);
    }
}

// 后序遍历并将结果存入数组
void print_tree(tree *node, int *print, int *place)
{
    if (!node)
        return;
    print_tree(node->left, print, place);
    print_tree(node->right, print, place);
    print[(*place)++] = node->data;
}

int main()
{
    // 输入数据
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &pre[i]);
    }

    // 创建根节点
    tree *t = create_node(pre[0]);

    // 尝试建立普通BST
    stablish(t, 1, n - 1, 0);

    // 如果不符合BST，再尝试建立镜像BST
    if (!t->left && !t->right)
    {
        free_tree(t);
        t = create_node(pre[0]);
        stablish(t, 1, n - 1, 1);
    }

    // 判断是否构建成功
    if (!t->left && !t->right)
    {
        printf("NO");
    }
    else
    {
        printf("YES\n");

        // 输出后序遍历
        int print[1005], place = 0;
        print_tree(t, print, &place);

        for (int i = 0; i < place - 1; i++)
        {
            printf("%d ", print[i]);
        }
        printf("%d", print[place - 1]);
    }

    // 释放二叉树的内存
    free_tree(t);
    return 0;
}

